package com.LFLWZ.controller;


import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.POJO.Comments;
import com.LFLWZ.beans.POJO.Worksshow;
import com.LFLWZ.beans.Vo.CommentsByReplyVo;
import com.LFLWZ.beans.Vo.CommentsVo;
import com.LFLWZ.beans.Vo.WorksshowAndComments;
import com.LFLWZ.service.CommentsService;
import com.LFLWZ.service.worksService;
import com.LFLWZ.utils.DtoUtil;
import com.LFLWZ.utils.EmptyUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("works")
public class worksController {

    @Reference
    private worksService worksService;
    @Reference
    private CommentsService commentsService;

    @PostMapping("addWorks")
    public Dto addWorks(@RequestBody Worksshow Worksshow){
        Dto dto= null;
        if(Worksshow.getId()!=null &&Worksshow.getId()!=0){
             int line=worksService.update(Worksshow);
             if (line>0){
                 return DtoUtil.returnSuccessData(line);
             }
        }
        if (EmptyUtils.isEmpty(Worksshow)){
            dto= DtoUtil.returnError(4001,"传输的数据为空");
        }
        int line=worksService.addWorks(Worksshow);
        if(line>0){
            dto=DtoUtil.returnSuccessData(line);
        }else {
            dto=DtoUtil.returnError(4002,"传输失败");
        }
        return dto;
    }

    /**
     *
     * @param uid 用户id
     * @param special 1：作品。2：日记
     * @param name 标签
     * @param audit 审核状态
     * @return
     */
    @GetMapping("selWorks/{uid}/{special}/{name}/{audit}")
    public Dto selWorks(@PathVariable Integer uid,@PathVariable Integer special,@PathVariable String name,@PathVariable Integer audit){
        Dto dto=null;
        if (EmptyUtils.isEmpty(uid))
            dto=DtoUtil.returnError(4001,"参数不能为空");
        Worksshow w=new Worksshow();
       if(uid!=0)
           w.setUid(uid);
       if (!name.equals(' ')&&!name.equals("0")){
           w.setTags(name);
       }
       if (audit>=0){
           w.setAudit(audit);
       }
       w.setSpecial(special);
        List<Worksshow> worksshows= worksService.selWorks(w);
        if (EmptyUtils.isEmpty(worksshows)){
            dto=DtoUtil.returnError(4002,"查询失败");
        }else {
            List<CommentsByReplyVo> l=new ArrayList<>();
            Comments comments=new Comments();
            if (special==1){
                comments.setType("works");
            }else if (special==2){
                comments.setType("diary");
            }
            List<CommentsByReplyVo> list= commentsService.queryAll(comments);
            List<WorksshowAndComments> worksshowAndComments=new ArrayList<>();
            for (Worksshow wk:worksshows){
                    //把实体类的数据存入vo中
                    WorksshowAndComments vo=new WorksshowAndComments();
                    l=new ArrayList<>();
                    for (CommentsByReplyVo c:list){
                        if(wk.getId()==c.getAid()){
                            l.add(c);
                        }
                    }
                List<Worksshow> listW=worksService.selCountTime(2,wk.getUid());
                    vo.setComments(l);
                    vo.setCombo(listW.size());
                    BeanUtils.copyProperties(wk, vo);
                    worksshowAndComments.add(vo);
            }
            dto=DtoUtil.returnSuccessData(worksshowAndComments);
        }

        return dto;
    }

    @GetMapping("selByid/{id}")
    public Dto selByid(@PathVariable Integer id){
        Dto dto=null;

        Worksshow Worksshow= worksService.sqlById(id);
        if (EmptyUtils.isEmpty(Worksshow)){
            dto=DtoUtil.returnError(4001,"查询数据为空");
        }else {
            dto=DtoUtil.returnSuccessData(Worksshow);
        }

        return dto;
    }
    @PostMapping("upWorksByViews")
    public Dto upWorksByViews(@RequestBody  Worksshow worksshow){
        Dto dto=null;
        if (EmptyUtils.isEmpty(worksshow.getViews())&&EmptyUtils.isEmpty(worksshow.getId()))
            dto=DtoUtil.returnError(40001,"参数不能为空");
        worksshow.setViews(worksshow.getViews()+1);
        int line= worksService.update(worksshow);
        if (line>0)
            dto=DtoUtil.returnSuccessData(line);
        else
            dto=DtoUtil.returnError(4002,"执行失败");
        return dto;
    }

    @GetMapping("delete/{id}")
    public Dto delete(@PathVariable Integer id){
        Dto dto=null;
        if (EmptyUtils.isEmpty(id)){
            dto=DtoUtil.returnError(4001,"id不能为空");
        }
        int line= worksService.deleteById(id);
        if (line>0){
            dto=DtoUtil.returnSuccessData(id);
        }else {
            dto=DtoUtil.returnError(4002,"查询失败");
        }
        return dto;
    }

//    @GetMapping("selCountTime/{type}")
//    public Dto selCountTime(@PathVariable Integer type){
//        Dto dto=null;
//        if(EmptyUtils.isEmpty(type))
//            dto=DtoUtil.returnError(4001,"参数为空");
//        List<Worksshow> worksshows= worksService.selCountTime(type);
//        if (EmptyUtils.isEmpty(worksshows)) {
//            dto = DtoUtil.returnError(4001, "查询为空");
//        }else {
//            dto = DtoUtil.returnSuccessData(worksshows.size());
//        }
//        return dto;
//    }
    @GetMapping("deleteByid/{id}")
    public Dto deleteByid(@PathVariable Integer id){
        Dto dto=null;
        if (id==null){
            dto=DtoUtil.returnError(4001,"参数为空");
        }
        int line=worksService.deleteById(id);
        int line2= commentsService.deleteByAid(id);
        if (line>0){
            dto=DtoUtil.returnSuccessData(line);
        }else {
            dto=DtoUtil.returnError(4002,"删除失败");
        }
        return dto;
    }
    @GetMapping("deleteByAid/{aid}")
    public Dto deleteByAid(@PathVariable Integer aid){
        Dto dto=null;
        if (aid==null){
            dto=DtoUtil.returnError(4001,"参数错误");
        }
        Boolean line= commentsService.deleteById(aid);
        if (line)
            dto=DtoUtil.returnSuccessData(line);
        else
            dto=DtoUtil.returnError(4002,"删除错误");
        return dto;
    }
}
